﻿
using RevitAddInDeployer;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;


namespace ZhuXingFamilys.Help
{
    /// <summary>
    /// Setting.xaml 的交互逻辑
    /// </summary>
    public partial class Set : Window
    {
        public Set()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            try
            {
                string path = Set.getaddress();
                string paths = System.IO.Path.Combine(path.Substring(0, path.Length ), "image\\设置.ico");
                Uri Icon = new Uri(paths, UriKind.Absolute);
                this.Icon = System.Windows.Media.Imaging.BitmapFrame.Create(Icon);
            }
            finally { }
            address.Text = settingaddress("Setup.ini");
        }

        //选择文件夹
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            FolderBrowserDialog openFileDialog = new FolderBrowserDialog(); //选择文件夹
            //注意，此处一定要手动引入System.Window.Forms空间，否则你如果使用默认的DialogResult会发现没有OK属性
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                address.Text = openFileDialog.SelectedPath;
            }
        }

        public static string CurAppDir = "";
        public const string CONFIG_SETUP_CACHE_CONTENT = "SetupCacheContent";
        public const string ADDRESS = "Address";
        public const string SETUPCOUNT = "SetupCount";
        public static string iniFilePath = "Setup.ini";
       
        [DllImport("kernel32.dll")]
        public static extern long WritePrivateProfileString(string section, string key, string value, string filepath);

        [DllImport("kernel32.dll")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder returnvalue, int buffersize, string filepath);
        public static string addresstext;
        //读取ini文件中得地址
        public static  string settingaddress(string iniFilePath)
        {
            CurAppDir = getaddress();
            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            {
                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            }
            if (GetValue(CONFIG_SETUP_CACHE_CONTENT, SETUPCOUNT, iniFilePath, out string countvalue) == "1")
            {
                addresstext = GetValue(CONFIG_SETUP_CACHE_CONTENT, ADDRESS, iniFilePath, out string value);
            }
            else {
                //WritePrivateProfileString(CONFIG_SETUP_CACHE_CONTENT, SETUPCOUNT, "1", iniFilePath);
                addresstext = @"D:\ZhuXingFamilyFile";
            }
            return addresstext;
        }
        //读取ini文件
        public static string GetValue(string section, string key, string IniFilePath, out  string value)
        {
            StringBuilder stringBuilder = new StringBuilder();
            GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath);
            value = stringBuilder.ToString() ?? "";
            return value;
        }
        //确定按钮
        private  void Button_Click1(object sender, RoutedEventArgs e)
        {
            CurAppDir = getaddress();
            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            {
                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            }
            string Oldaddress = settingaddress("Setup.ini");
            if (Directory.Exists(Oldaddress))
            {
                DirectoryInfo di = new DirectoryInfo(Oldaddress);
                di.Delete(true);

            }
            
            WritePrivateProfileString(CONFIG_SETUP_CACHE_CONTENT, SETUPCOUNT,"1", iniFilePath);
            WritePrivateProfileString(CONFIG_SETUP_CACHE_CONTENT, ADDRESS, address.Text.ToString(), iniFilePath);
            string a = FtpHelper.path;
            string b = "";
            string c = address.Text.ToString();
            Task task = Task.Run(() =>
            {
                FtpHelper.Downftp(a, b, c);
            });
            
            this.Close();
        }
        //取消按钮
        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //取得addin中得安装路径 测试版本需要去掉2021
        public static string getaddress()
        {
            string path = @"C:\ProgramData\Autodesk\Revit\Addins\2021\", fileName = "@ZhuXingFamilys.addin";
            string[] files = Directory.GetFiles(path, fileName, System.IO.SearchOption.AllDirectories);
            string a = files[0];
            StreamReader sr = new StreamReader(a, Encoding.UTF8);
            String ls_input = sr.ReadToEnd().TrimStart();
            //MessageBox.Show(ls_input);

            string address = ls_input.Split(new string[] { "<Assembly>", "ZhuXingFamilys.dll</Assembly>" }, StringSplitOptions.RemoveEmptyEntries)[1];
            //address = address.Substring(0, address.Length - 20);
            //string address = ls_input.Split(new string[] { "<Assembly>", "ZhuXingFamilys\\2021\\ZhuXingFamilys.dll</Assembly>" }, StringSplitOptions.RemoveEmptyEntries)[1];
            sr.Close();
            return address;
        }
    }
}
